using System;
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;

namespace AdvancedInspector
{
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = true)]
	public class HelpAttribute : Attribute, IRuntimeAttribute, IRuntimeAttribute<HelpAttribute>
	{
		public delegate HelpAttribute HelpDelegate();

		private HelpType type;

		private string message;

		private HelpPosition position;

		private string methodName = string.Empty;

		private List<Delegate> delegates = new List<Delegate>();

		public HelpType Type
		{
			get
			{
				return type;
			}
			set
			{
				type = value;
			}
		}

		public string Message
		{
			get
			{
				return message;
			}
			set
			{
				message = value;
			}
		}

		public HelpPosition Position
		{
			get
			{
				return position;
			}
			set
			{
				position = value;
			}
		}

		public string MethodName
		{
			get
			{
				return methodName;
			}
		}

		public Type Template
		{
			get
			{
				return typeof(HelpDelegate);
			}
		}

		public List<Delegate> Delegates
		{
			get
			{
				return delegates;
			}
			set
			{
				delegates = value;
			}
		}

		public HelpAttribute(string methodName)
		{
			this.methodName = methodName;
		}

		public HelpAttribute(HelpType type, string message)
		{
			this.type = type;
			this.message = message;
		}

		public HelpAttribute(HelpType type, HelpPosition position, string message)
		{
			this.type = type;
			this.position = position;
			this.message = message;
		}

		public HelpAttribute(Delegate method)
		{
			delegates.Add(method);
		}

		public HelpAttribute Invoke(int index)
		{
			if (delegates.Count == 0 || index >= delegates.Count)
			{
				return this;
			}
			try
			{
				return delegates[0].DynamicInvoke() as HelpAttribute;
			}
			catch (Exception innerException)
			{
				if (innerException is TargetInvocationException)
				{
					innerException = ((TargetInvocationException)innerException).InnerException;
				}
				Debug.LogError(string.Format("Invoking a method failed while trying to retrieve a Help attribute. The exception was \"{0}\".", innerException.Message));
				return null;
			}
		}
	}
}
